package org.ithink.util;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
  /**
   * Date类型转换
   */
  public static Date parseDate(String time) {
    /** */
    /**
     * 字符串转换为java.util.Date 支持格式为: yyyy.MM.dd G 'at' hh:mm:ss z 如 '2002-1-1 AD
     * at 22:10:59 PSD' yy/MM/dd HH:mm:ss 如 '2002/1/1 17:55:00' yy/MM/dd
     * HH:mm:ss pm 如 '2002/1/1 17:55:00 pm' yy-MM-dd HH:mm:ss 如 '2002-1-1
     * 17:55:00' yy-MM-dd HH:mm:ss am 如 '2002-1-1 17:55:00 am'
     * 
     * @param time String 字符串
     * @return Date 日期
     */
    try {
      SimpleDateFormat formatter;

      int tempPos = time.indexOf("AD");
      time = time.trim();
      formatter = new SimpleDateFormat(" yyyy.MM.dd G 'at' hh:mm:ss z ");
      if (tempPos > -1) {
        time = time.substring(0, tempPos) + " 公元 " + time.substring(tempPos + " AD ".length()); // china
        formatter = new SimpleDateFormat(" yyyy.MM.dd G 'at' hh:mm:ss z ");
      }

      tempPos = time.indexOf("-");

      if (time.indexOf(".") != -1) {
        time = time.substring(0, time.indexOf("."));
      }

      if (tempPos > -1) {// 包含“-”
        if (time.indexOf(":") == -1) {
          formatter = new SimpleDateFormat("yyyy-MM-dd");
        } else if (time.indexOf(":") == time.lastIndexOf(":")) {
          formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        } else {
          formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
      } else if (time.indexOf("/") > -1) {// 包含“/”
        if (time.indexOf(":") == -1) {
          formatter = new SimpleDateFormat("yyyy/MM/dd");
        } else {
          formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        }
      } else if (time.substring(0, 8).indexOf("-") == -1) {
        time = time.substring(0, 8);
        formatter = new SimpleDateFormat("yyyyMMdd");
      } else {
        formatter = new SimpleDateFormat("HH:mm");
      }

      ParsePosition pos = new ParsePosition(0);
      java.util.Date ctime = formatter.parse(time, pos);
      return ctime;

    } catch (Exception ex) {
      return null;
    }
  }
  
  /**
   * Data格式化
   */
  public static String dateFormat(Date date, String pattern) {
    SimpleDateFormat df = new SimpleDateFormat(pattern);
    try {
      return df.format(date);
    } catch (Exception e) {
      return "";
    }
  }

  /**
   * 得到月初
   */
  public static Date getFirstDayOfMonth(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
    return getCurrentDate(cal.getTime());
  }

  /**
   * 得到月末
   */
  public static Date getLastDayOfMonth(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
    return getCurrentDate(cal.getTime());
  }

  /**
   * 获取当前日期前几天
   */
  public static Date addDayOfMonth(Date date, int days) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.add(Calendar.DATE, days);
    return getCurrentDate(calendar.getTime());
  } 

  /**
   * 处理当前日期去掉小时分钟秒
   */
  public static Date getCurrentDate(Date date) {
    return parseDate(dateFormat(date, "yyyy-MM-dd"));
  }
  
  /**
   * 1、比较两个日期差多少天
   * @param args
   */

  public static void main(String[] args){
    System.out.println(dateFormat(addDayOfMonth(new Date(), 1), "yyyy-MM-dd"));
  }
  
  public static String YYYY_MM_DD = "yyyy-MM-dd";
  public static String YYYYMMDD = "yyyyMMdd";
  public static String YYYYMM = "yyyyMM";
  public static String YYYY_MM = "yyyy-MM";
  public static String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
  public static String YYYYMMDDHHMM = "yyyyMMddHHmm";
  public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
  public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

  /**
   * 将字符串时间改成Date类型
   */
  public static Date strToDate(String dateStr, String format) {

    Date date = null;

    try {
      SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
      date = simpleDateFormat.parse(dateStr);
    } catch (ParseException e) {
      e.printStackTrace();
    }

    return date;
  }

  /**
   * 将Date时间转成字符串
   */
  public static String DateToStr(String format, Date date) {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
    return simpleDateFormat.format(date);
  }

  /**
   * 获取2个字符日期的天数差
   * @return 天数差
   */
  public static long getDaysOfTowDiffDate(String p_startDate, String p_endDate) {

    Date l_startDate = DateUtil.strToDate(DateUtil.YYYY_MM_DD, p_startDate);
    Date l_endDate = DateUtil.strToDate(DateUtil.YYYY_MM_DD, p_endDate);
    long l_startTime = l_startDate.getTime();
    long l_endTime = l_endDate.getTime();
    long betweenDays = (long) ((l_endTime - l_startTime) / (1000 * 60 * 60 * 24));
    return betweenDays;
  }

  /**
   * 获取2个Date型日期的分钟数差值
   * @param p_startDate
   * @param p_endDate
   * @return 分钟数差值
   */
  public static long getMinutesOfTowDiffDate(Date p_startDate, Date p_endDate) {
    long l_startTime = p_startDate.getTime();
    long l_endTime = p_endDate.getTime();
    long betweenMinutes = (long) ((l_endTime - l_startTime) / (1000 * 60));
    return betweenMinutes;
  }

  /**
   * 获取2个字符日期的天数差
   * @param p_startDate
   * @param p_endDate
   * @return 天数差
   */
  public static long getDaysOfTowDiffDate(Date l_startDate, Date l_endDate) {

    long l_startTime = l_startDate.getTime();
    long l_endTime = l_endDate.getTime();
    long betweenDays = (long) ((l_endTime - l_startTime) / (1000 * 60 * 60 * 24));
    return betweenDays;
  }

  /**
   * 给出日期添加一段时间后的日期
   * 
   * @param dateStr
   * @param plus
   * @return
   */
  public static String getPlusDays(String format, String dateStr, long plus) {

    Date date = DateUtil.strToDate(format, dateStr);
    long time = date.getTime() + plus * 24 * 60 * 60 * 1000;

    return DateUtil.DateToStr(format, new Date(time));
  }

  /**
   * 给出日期添加一段时间后的日期
   */
  public static String getPlusDays(String format, Date date, long plus) {

    long time = date.getTime() + plus * 24 * 60 * 60 * 1000;

    return DateUtil.DateToStr(format, new Date(time));
  }
}
